package com.pine.demo;

import com.pine.common.EfficiencyTest;
import com.pine.common.GeneratorArr;

/**
 * 通用的数组查找类
 * 线性查找法
 */
public class LinearSearch {

    public static  <E> int search(E[] es, E e) {
        // 查找逻辑
        for (int i = 0; i < es.length; i++) {
            if (es[i].equals(e)) {
                return i;
            }
        }
        // 如果查不到则返回-1
        return -1;
    }

    public static void main(String[] args) {
        int n = 1000000;
        int num = 100;
        Integer[] data = GeneratorArr.generatorIntegerArr(n);
        double result = EfficiencyTest.nanoTimeTest(num, () -> {
            int i = LinearSearch.search(data, n);
        });
        System.out.printf("循环测试%s次，总耗时为%s秒\n", num, result);
    }
}
